package com.yjd.bi.authority.vo;

import com.yjd.bi.authority.model.AuthorityManagementModel;
import com.yjd.bi.authority.service.IAuthorityManagementService;
import com.yjd.bi.comm.vo.Constant;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.rpm.RpmServiceKeyEnum;
import com.yjd.comm.util.Assert;
import com.yjd.comm.util.ServiceHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限管理
 */
public class AuthorityUtil {

    /**
     * 生成权限列表
     */
    public static List<UserAuthorityDto> generateAuthorityList(int authority){
        List<UserAuthorityDto> athityList = new ArrayList<UserAuthorityDto>();
        for (Constant.AuthorityEnum ath : Constant.AuthorityEnum.values()) {
            if((ath.getAuthority() & authority) == ath.getAuthority()){
                athityList.add(new UserAuthorityDto(ath.getDescription(), ath.getAuthority(), true));
                continue;
            }
            athityList.add(new UserAuthorityDto(ath.getDescription(), ath.getAuthority(), false));
        }
        return athityList;
    }

    /**
     * 判断用户是否具有权限
     * true：有，false：没有
     */
    public static boolean authorityValidate(Long admin_id, Constant.AuthorityEnum ath) throws Exception {

        IAuthorityManagementService athityMgr = ServiceHolder.getService(IAuthorityManagementService.class, "authorityManagementService", RpmServiceKeyEnum.RPM_BI_SERVICE_KEY_E);
        Assert.notNull(athityMgr, "系统繁忙，请稍后重试。");

        AuthorityManagementModel amm = athityMgr.getModelById(admin_id, DbWREnums.READ);
        Assert.notNull(amm, "查询不到用户信息。");

        if((ath.getAuthority() & amm.getAuthority()) == ath.getAuthority()){
            return true;
        }

        return false;
    }

}
